package net.danielthompson.danray.structures;

/**
 * DanRay
 * User: dthompson
 * Date: 6/27/13
 * Time: 1:49 PM
 */
public class Vector {
   public Point Direction;
   public Point Origin;

   /**
    * Creates a new Vector object.
    * @param origin The origin of the vector.
    * @param direction The absolute direction of the vector (i.e. not relative to the origin). Will be normalized.
    */
   public Vector(Point origin, Point direction) {
      Origin = origin;

      double length = Math.sqrt(direction._tuple.getX() * direction._tuple.getX() + direction._tuple.getY() * direction._tuple.getY() + direction._tuple.getZ() * direction._tuple.getZ());
      Direction = new Point(direction._tuple.getX() / length, direction._tuple.getY() / length, direction._tuple.getZ() / length);
   }

   public Point ScaleFromOrigin(double t) {
      double x = Origin._tuple.getX() + t * Direction._tuple.getX();
      double y = Origin._tuple.getY() + t * Direction._tuple.getY();
      double z = Origin._tuple.getZ() + t * Direction._tuple.getZ();

      return new Point(x, y, z);
   }

   public Point Scale(double t) {
      return new Point(Tuple.Scale(Direction._tuple, t));
   }

   public boolean equals(Object obj) {
      if (obj == null)
         return false;
      if (obj == this)
         return true;
      if (!(obj instanceof Vector))
         return false;

      Vector rhs = (Vector) obj;

      return (Direction.equals(rhs.Direction) && Origin.equals(rhs.Origin));
   }

   public String toString() {
      return "Origin: " + Origin + ", Direction: " + Direction;
   }
}
